DAY5:Delete occurrences of an element if it occurs more than n times


Posted by birdbirdmurmur on 2023-07-18

題目連結:

Delete occurrences of an element if it occurs more than n times

解題思維:

  1. 要有一個儲存數字出現次數的「物件」
  2. 對arr遍歷篩選
  3. 給物件一個屬性
  4. 數字重複就+1 沒有就從1開始
  5. 直到n回傳

實作解法:

function deleteNth(arr,n){
  let count = {};
   return arr.filter( num =>{
     count[num] = (count[num] || 0) +1
     return count[num] <= n
})
}

心得

這題真的超難!
爬完discourse只稍微整理出一點邏輯
我想我目前的程度還不足以挑戰6kyu等級......
連續兩天在6kyu花了太多時間
接下來還是先回頭到7kyu好了


#javascript #Codewars #filter #object







Related Posts

Airflow 動手玩:(二)動手寫 DAG

Airflow 動手玩:(二)動手寫 DAG

[C#] .net framework 使用 Autofac 註冊多組資料庫連線

[C#] .net framework 使用 Autofac 註冊多組資料庫連線

Fetch 與 Promise (五):async 與 await

Fetch 與 Promise (五):async 與 await


Comments